package edu.purdue.spm.component;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import edu.purdue.spm.R;

public class GroupTaskTreeStructureFragment extends Fragment{

	static View base;
	
	Button root_node;
	
	RelativeLayout levels[];
	
	LinearLayout horizontal_linear_layout;
	ScrollView sv;
	
	
	String group_task_infos[];
	
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		
		base = inflater.inflate(R.layout.group_task_tree_structure, container, false);
		
		sv = (ScrollView) base.findViewById(R.id.scrollView1);
		
		levels = new RelativeLayout[9];
		
		levels[0] = (RelativeLayout) base.findViewById(R.id.relativelayout0);
		levels[1] = (RelativeLayout) base.findViewById(R.id.relativelayout1);
		levels[2] = (RelativeLayout) base.findViewById(R.id.relativelayout2);
		levels[3] = (RelativeLayout) base.findViewById(R.id.relativelayout3);
		levels[4] = (RelativeLayout) base.findViewById(R.id.relativelayout4);
		levels[5] = (RelativeLayout) base.findViewById(R.id.relativelayout5);
		levels[6] = (RelativeLayout) base.findViewById(R.id.relativelayout6);
		levels[7] = (RelativeLayout) base.findViewById(R.id.relativelayout7);
		levels[8] = (RelativeLayout) base.findViewById(R.id.relativelayout8);
		
		root_node = new Button(base.getContext());
		root_node.setBackgroundDrawable(getResources().getDrawable(R.drawable.treenodered));
		root_node.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	levels[1].setVisibility(View.VISIBLE);
            	System.out.println("sv size: " + sv.getHeight() +"  " + sv.getWidth());
            }
        });
		
		LinearLayout.LayoutParams node_layoutParams = new LinearLayout.LayoutParams(
			     LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
		node_layoutParams.setMargins(0, 275, 0, 0);
		root_node.setText("Root");
		root_node.setTextSize(12);
		LinearLayout vertial_linear_layout = new LinearLayout(base.getContext());
		vertial_linear_layout.setOrientation(LinearLayout.VERTICAL);
		vertial_linear_layout.addView(root_node, node_layoutParams);
		levels[0].addView(vertial_linear_layout);
		
		/*ViewGroup.LayoutParams vg_lp = root_node.getLayoutParams();
		RelativeLayout.LayoutParams rl_lp = new RelativeLayout.LayoutParams(vg_lp);
        rl_lp.addRule(RelativeLayout.CENTER_VERTICAL);
        root_node.setLayoutParams(rl_lp);
		*/

		try {
			drawTree();
			drawLevel2();
		}catch(Exception e) {
			
		}
		
        return base;
    }

	public void initialize(String [] group_tasks){
		//TASK ID:;:Parent Task ID;:;Owner_id:;:Title;:;Content:;:Due Time:;:Progress:;:TYPE :;:Weight:;:Depth:;:TimeStamp
		String dummy_data[] = {
				"161366587228:;:0:;:Di Xu:;:Server Implementation:;:Set up the connection between server and client, and realize the basic functionalities of the server:;:2013-04-22 19:33:00.0:;:0:;:G:;:1:;:M:;:2013-04-21 19:33:45.0",
				"161366587229:;:0:;:Binhao Lin:;:Client Implementation:;:Set up the connection between server and client, and realize the basic functionalities of the client:;:2013-04-22 19:33:00.0:;:0:;:G:;:1:;:M:;:2013-04-21 19:33:45.0",
				"161366587230:;:0:;:James:;:UI design:;:Basic xml implementation:;:2013-04-22 19:33:00.0:;:0:;:G:;:1:;:L:;:2013-04-21 19:33:45.0",
				"161366587231:;:161366587228:;:Tracy:;:Server Implementation subtask 1:;:Database Implementation:;:2013-04-22 19:33:00.0:;:0:;:G:;:1:;:M:;:2013-04-21 19:33:45.0",
				"161366587232:;:161366587228:;:Tracy:;:Server Implementation subtask 2:;:Server and Client protocal:;:2013-04-22 19:33:00.0:;:0:;:G:;:1:;:L:;:2013-04-21 19:33:45.0",
				"161366587233:;:161366587229:;:Tracy:;:Server Implementation subtask 3:;:Multi thread implementation:;:2013-04-22 19:33:00.0:;:0:;:G:;:1:;:L:;:2013-04-21 19:33:45.0",
				"161366587234:;:161366587229:;:Binhao Lin:;:Client Implementation subtask1:;:Register/Login functionality:;:2013-04-22 19:33:00.0:;:0:;:G:;:1:;:L:;:2013-04-21 19:33:45.0",
				"161366587235:;:161366587229:;:Binhao Lin:;:Client Implementation subtask2:;:Creating group function:;:2013-04-22 19:33:00.0:;:0:;:G:;:1:;:L:;:2013-04-21 19:33:45.0"
		};

		//group_task_infos = group_tasks;
		group_task_infos = dummy_data;
		
		try {
			drawTree();
			drawLevel2();
		}catch(Exception e) {
			
		}
	}
	
	public void drawTree() {
		int task_ct = 1;
		
		LinearLayout.LayoutParams first_node_layoutParams = new LinearLayout.LayoutParams(
			     LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
		first_node_layoutParams.setMargins(0, 150, 0, 30);
			
		LinearLayout.LayoutParams node_layoutParams = new LinearLayout.LayoutParams(
			     LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
		node_layoutParams.setMargins(0, 30, 0, 30);
		
		LinearLayout vertial_linear_layout = new LinearLayout(base.getContext());
		vertial_linear_layout.setOrientation(LinearLayout.VERTICAL);
		
		
		//Draw level1 nodes
		for(int i=0; i<group_task_infos.length; i++) {
			String params[] = group_task_infos[i].split(":;:");
			String parent_task_id = params[1];
			if(parent_task_id.equals("0")){
				Button node = new Button(base.getContext());
				node.setText("Task"+task_ct);
				node.setTextSize(12);
				task_ct++;
				if(params[9].equals("M")) {
					//middle node
					node.setBackgroundDrawable(getResources().getDrawable(R.drawable.treenodered));
					if(task_ct == 2) {
						node.setOnClickListener(new View.OnClickListener() {
							public void onClick(View v) {
								LinearLayout l = (LinearLayout) levels[2].getChildAt(0);

								l.getChildAt(0).setVisibility(View.VISIBLE);
								l.getChildAt(1).setVisibility(View.VISIBLE);
								l.getChildAt(2).setVisibility(View.VISIBLE);
							}
						});
					} else if (task_ct == 3) {
						node.setOnClickListener(new View.OnClickListener() {
							public void onClick(View v) {
								LinearLayout l = (LinearLayout) levels[2].getChildAt(0);

								l.getChildAt(3).setVisibility(View.VISIBLE);
								l.getChildAt(4).setVisibility(View.VISIBLE);
							}
						});
					}
				} else if (params[9].equals("L")){
					//leaf node
					node.setBackgroundDrawable(getResources().getDrawable(R.drawable.treenodeblue));
				}
				if(i == 0)
					vertial_linear_layout.addView(node, first_node_layoutParams);
				else
					vertial_linear_layout.addView(node, node_layoutParams);
				
				setLongClickListener(node, group_task_infos[i]);
			}
		}
		levels[1].addView(vertial_linear_layout);
		levels[1].setVisibility(View.INVISIBLE);
		
		
		ViewGroup.LayoutParams vg_lp = vertial_linear_layout.getLayoutParams();
		RelativeLayout.LayoutParams rl_lp = new RelativeLayout.LayoutParams(vg_lp);
        rl_lp.addRule(RelativeLayout.CENTER_VERTICAL);
        vertial_linear_layout.setLayoutParams(rl_lp);

	}
	
	public void drawLevel2(){
		//Draw level2 nodes
		int task_ct = 4;
		
		LinearLayout.LayoutParams first_node_layoutParams = new LinearLayout.LayoutParams(
			     LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
		first_node_layoutParams.setMargins(0, 80, 0, 0);
			
		
		LinearLayout vertial_linear_layout = new LinearLayout(base.getContext());
		vertial_linear_layout.setOrientation(LinearLayout.VERTICAL);
		
		
        for(int i=3; i<6; i++) {
			String params[] = group_task_infos[i].split(":;:");
			String parent_task_id = params[1];
			if(!parent_task_id.equals("0")){
				Button node = new Button(base.getContext());
				node.setText("Task"+task_ct);
				node.setTextSize(12);
				node.setVisibility(View.INVISIBLE);
				task_ct++;
				if(params[9].equals("M")) {
					//middle node
					node.setBackgroundDrawable(getResources().getDrawable(R.drawable.treenodered));
					
				} else if (params[9].equals("L")){
					//leaf node
					node.setBackgroundDrawable(getResources().getDrawable(R.drawable.treenodeblue));
				}
				if(i == 3)
					vertial_linear_layout.addView(node, first_node_layoutParams);
				else
					vertial_linear_layout.addView(node);
				
				setLongClickListener(node, group_task_infos[i]);
			}
        }
        
        
        first_node_layoutParams.setMargins(0, 20, 0, 0);
        for(int i=6; i<8; i++) {
			String params[] = group_task_infos[i].split(":;:");
			String parent_task_id = params[1];
			if(!parent_task_id.equals("0")){
				Button node = new Button(base.getContext());
				node.setText("Task"+task_ct);
				node.setTextSize(12);
				node.setVisibility(View.INVISIBLE);
				task_ct++;
				if(params[9].equals("M")) {
					//middle node
					node.setBackgroundDrawable(getResources().getDrawable(R.drawable.treenodered));
					
				} else if (params[9].equals("L")){
					//leaf node
					node.setBackgroundDrawable(getResources().getDrawable(R.drawable.treenodeblue));
				}
				if(i == 6)
					vertial_linear_layout.addView(node, first_node_layoutParams);
				else
					vertial_linear_layout.addView(node);
				
				setLongClickListener(node, group_task_infos[i]);
			}
        }
        
        
        levels[2].addView(vertial_linear_layout);
	}

	public void setLongClickListener(Button btn, final String task_info){
		btn.setOnLongClickListener(new OnLongClickListener() { 
	        @Override
	        public boolean onLongClick(View v) {
	        	String params[] = task_info.split(":;:");
	        	String message = "Task detail: " + params[4] + "\n\n";
	        	message += "Assign to: " + params[2] +"\n\n";
	        	message += "Due time: " + params[5] + "\n\n";
	        	
	        	AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
	        	builder.setTitle(params[3]);
	        	builder.setMessage(message)
	        		.setPositiveButton("OK", new DialogInterface.OnClickListener() {
	        		public void onClick(DialogInterface dialog, int id) {
	        		}
	        		});
	        	builder.create();
	            builder.show();
	            
	            return true;
	        }
	    });
	}

	
}
